package com.w.contro;

import com.w.domain.Msg;
import com.w.util.ConnectJWGL;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@RestController
public class userContro {

    private Map<String,String> cookies;

    @RequestMapping("/tiao")
    public ModelAndView tiao(){
        ModelAndView m = new ModelAndView();
        m.setViewName("studentLogin");
        return m;
    }

    @PostMapping("stuLogin")
    public ModelAndView userLogin(String username, String password){
        ModelAndView m = new ModelAndView();
        ConnectJWGL c = new ConnectJWGL(username,password);
        try {
            Map<String, String> init = c.init();
            if (!init.get("JSESSIONID").equalsIgnoreCase("error")){
                cookies=init;
                m.setViewName("student");
                m.addObject("msg",Msg.success().add("msg","登陆成功"));
                return m;
            }else {
                m.setViewName("studentLogin");
                m.addObject("msg",init.get("msg"));
                return m;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        m.addObject("msg","异常");
        return m;
    }

    @RequestMapping("getStuInfo")
    public Msg studentInfo()  {
        if (cookies == null) {
            return Msg.fail().add("msg","必须先登陆！！！");
        }
        Map studentInfo = ConnectJWGL.getStudentInfo(2021, 1, cookies);
        return studentInfo == null ? Msg.fail():Msg.success().add("studentInfo",studentInfo);
    }

    @RequestMapping("getStuTimetable")
    public Msg studentTimetable(int year , int term){
        if (cookies == null) {
            return Msg.fail().add("msg","必须先登陆！！！");
        }
        Map studentTimetable = ConnectJWGL.getStudentTimetable(year, term, cookies);
        return studentTimetable == null ? Msg.fail():Msg.success().add("学年&学期",year+"&"+term).add("studentTimetable",studentTimetable);
    }

}
